home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / nslookup / skip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-27  |  3.9 KB  |  167 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)skip.c    5.5 (Berkeley) 6/18/88";
  20. #endif /* not lint */
  21.  
  22. /*
  23.  *******************************************************************************
  24.  *
  25.  *  skip.c --
  26.  *
  27.  *    Routines to skip over portions of a query buffer.
  28.  *
  29.  *    Note: this file has been submitted for inclusion in
  30.  *    BIND resolver library. When this has been done, this file
  31.  *    is no longer necessary (assuming there haven't been any
  32.  *    changes).
  33.  *
  34.  *    Adapted from 4.3BSD BIND res_debug.c
  35.  *
  36.  *******************************************************************************
  37.  */
  38.  
  39. #include <sys/types.h>
  40. #include <netinet/in.h>
  41. #include <stdio.h>
  42. #include <arpa/nameser.h>
  43.  
  44. char *res_skip_rr();
  45.  
  46.  
  47. /*
  48.  *******************************************************************************
  49.  *
  50.  *  res_skip --
  51.  *
  52.  *     Skip the contents of a query.
  53.  *
  54.  *     Interpretation of numFieldsToSkip argument:
  55.  *            res_skip returns pointer to:
  56.  *        1 ->  start of question records.
  57.  *        2 ->  start of authoritative answer records.
  58.  *        3 ->  start of additional records.
  59.  *        4 ->  first byte after end of additional records.
  60.  *
  61.  *   Results:
  62.  *    (address)    - success operation.
  63.  *      NULL         - a resource record had an incorrect format.
  64.  *
  65.  *******************************************************************************
  66.  */
  67.  
  68. char *
  69. res_skip(msg, numFieldsToSkip, eom)
  70.     char *msg;
  71.     int numFieldsToSkip;
  72.     char *eom;
  73. {
  74.     register char *cp;
  75.     register HEADER *hp;
  76.     register int tmp;
  77.     register int n;
  78.  
  79.     /*
  80.      * Skip the header fields.
  81.      */
  82.     hp = (HEADER *)msg;
  83.     cp = msg + sizeof(HEADER);
  84.  
  85.     /*
  86.      * skip question records.
  87.      */
  88.     if (n = ntohs(hp->qdcount) ) {
  89.         while (--n >= 0) {
  90.             tmp = dn_skipname(cp, eom);
  91.             if (tmp == -1) return(NULL);
  92.             cp += tmp;
  93.             cp += sizeof(u_short);    /* type     */
  94.             cp += sizeof(u_short);    /* class     */
  95.         }
  96.     }
  97.     if (--numFieldsToSkip <= 0) return(cp);
  98.  
  99.     /*
  100.      * skip authoritative answer records
  101.      */
  102.     if (n = ntohs(hp->ancount)) {
  103.         while (--n >= 0) {
  104.             cp = res_skip_rr(cp, eom);
  105.             if (cp == NULL) return(NULL);
  106.         }
  107.     }
  108.     if (--numFieldsToSkip == 0) return(cp);
  109.  
  110.     /*
  111.      * skip name server records
  112.      */
  113.     if (n = ntohs(hp->nscount)) {
  114.         while (--n >= 0) {
  115.             cp = res_skip_rr(cp, eom);
  116.             if (cp == NULL) return(NULL);
  117.         }
  118.     }
  119.     if (--numFieldsToSkip == 0) return(cp);
  120.  
  121.     /*
  122.      * skip additional records
  123.      */
  124.     if (n = ntohs(hp->arcount)) {
  125.         while (--n >= 0) {
  126.             cp = res_skip_rr(cp, eom);
  127.             if (cp == NULL) return(NULL);
  128.         }
  129.     }
  130.  
  131.     return(cp);
  132. }
  133.  
  134.  
  135. /*
  136.  *******************************************************************************
  137.  *
  138.  *  res_skip_rr --
  139.  *
  140.  *     Skip over resource record fields.
  141.  *
  142.  *   Results:
  143.  *    (address)    - success operation.
  144.  *      NULL         - a resource record had an incorrect format.
  145.  *******************************************************************************
  146.  */
  147.  
  148. char *
  149. res_skip_rr(cp, eom)
  150.     char *cp;
  151.     char *eom;
  152. {
  153.     int tmp;
  154.     int dlen;
  155.  
  156.     if ((tmp = dn_skipname(cp, eom)) == -1)
  157.         return (NULL);            /* compression error */
  158.     cp += tmp;
  159.     cp += sizeof(u_short);    /*     type     */
  160.     cp += sizeof(u_short);    /*     class     */
  161.     cp += sizeof(u_long);    /*     ttl     */
  162.     dlen = _getshort(cp);
  163.     cp += sizeof(u_short);    /*     dlen     */
  164.     cp += dlen;
  165.     return (cp);
  166. }
  167.